/***************************************************************************
 * Copyright 2012 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *      Carbone Matteo
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.authorisationService.rabbitmq;

import it.txt.ens.authorisationService.gui.standalone.util.ApplicationPropertiesRepository;

import java.io.IOException;

/**
 * This class sends RabbitMQ commands to {@link RabbitMQCommandExecutor}.
 * 
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT network.txtgroup.com)
 * @author Carbone Matteo
 *
 */
public class RabbitMQCommandSender {
	private String tipeCommand = null;
	
	public RabbitMQCommandSender(String tipCom){
		this.setTipeCommand(tipCom);

	}
	
	public void send() throws IOException{

		RabbitMQCommandExecutor st = new RabbitMQCommandExecutor();
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.startServer)){
			st.startServer();
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.stopServer)){
			st.stopServer();
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.statusServer)){
			st.statusServer();
		}
	}
	

	public void send(String[] command) throws IOException{
		RabbitMQCommandExecutor st = new RabbitMQCommandExecutor();
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.addUser)){
			String name = command[0];
			String pass = command[1];
			st.execScriptAddUser(name, pass);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.deleteUser)){
			String name = command[0];
			st.execScriptDeleteUser(name);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.cangePassUser)){
			String name = command[0];
			String newPass = command[1];
			st.execScriptCangePassUser(name, newPass);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.setAdmin)){
			String name = command[0];
			st.execScriptSetAdmin(name);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.deleteAdmin)){
			String name = command[0];
			st.execScriptClearAdmin(name);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.addVhost)){
			String vhost = command[0];
			st.execScriptAddVhost(vhost);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.deleteVhost)){
			String vhost = command[0];
			st.execScriptDeleteVhost(vhost);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.setPermissions)){
			String vhostPath = command[0];
			String user = command[1];
			String conf = command[2];
			String write = command[3];
			String read = command[4];
			
			st.execScriptSetPermissions(vhostPath, user, conf, write, read);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.clearPermissions)){
			String vhostPath = command[0];
			String user = command[1];
			
			st.execScriptClearPermissions(vhostPath, user);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.manualCommand)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listPermission)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listUser)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listExchange)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listQueues)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listBinding)){
			st.execScriptManualCommand(command);
		}
		
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listVhost)){
			st.execScriptManualCommand(command);
		}
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listConenction)){
			st.execScriptManualCommand(command);
		}
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listChannel)){
			st.execScriptManualCommand(command);
		}
		if(getTipeCommand().equalsIgnoreCase(ApplicationPropertiesRepository.listConsumers)){
			st.execScriptManualCommand(command);
		}
	}
	
	private String getTipeCommand() {
		return tipeCommand;
	}

	private void setTipeCommand(String tc) {
		tipeCommand = tc;
	}
}
